package edu.npu.cv.controller;


import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.entity.ArticleDO;
import edu.npu.cv.entity.PaDO;

import edu.npu.cv.service.ArticleService;
import edu.npu.cv.service.PaService;
import edu.npu.cv.utils.MarkDownUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author lelechang
 * @description: 控制器层
 * @version: V1.0
 */
@RestController
@RequestMapping(TemplatePath.S + TemplatePath.PAS)
public class PAController {
    @Autowired
    private PaService paService;

    @Autowired
    private ArticleService articleService;


    /**
     * 从数据库获取公告列表
     */
    private List<ArticleDO> getPAList() {
        //return paService.findAll();
        return paService.findPa();
    }

    /**
     * 查询所有公告
     *
     * @return
     */
    @GetMapping
    public ModelAndView list(Model model) {
//        List<ArticleDO> list = getPAList();
//        //转换markerdown格式
//        for(int i=0;i<list.size();i++){
//            list.get(i).setContent(MarkDownUtil.mdToHtml(list.get(i).getContent()));
//        }
        model.addAttribute("paList", getPAList());
        return new ModelAndView("pa", "paModel", model);
    }

    /**
     * 根据ID查询用户
     *
     * @return
     */
    @GetMapping("{id}")
    public ModelAndView view(@PathVariable("id") String id, Model model) {
        ArticleDO pa = articleService.getArticleDOById(Integer.parseInt(id));
        //PaDO pa = paService.findById(id);
        pa.setContent(MarkDownUtil.mdToHtml(pa.getContent()));
        model.addAttribute("pa", pa);
        return new ModelAndView("paview", "paModel", model);
    }


}
